﻿
package src.utils
{
	import src.data.Categories;
	import src.data.Category;
	import src.data.MediaGroup;
	import src.data.SubSection;
	import src.debug.MonsterDebugger;
	
	public class MediaRSS 
	{
		/** Parse an RSS playlist for feeditems. **/
		public static function parse(dat:XML):Array 
		{
			var aCategory = new Array();
			var oCategory:Categories;
			
			for each (var i in dat.children())
			{
				oCategory = new Categories();
				
				oCategory.id    = i.attributes()[1];
				oCategory.title = i.attributes()[0];
				oCategory.subSection = parseCategory(i);
				
				aCategory.push(oCategory);
				
			}
			
			return aCategory;
		}
		
		public static function parseCategory(obj:XML):Array
		{
			var aSubArray = new Array();
			var oSubSection:SubSection
			
			for each(var i in obj.children())
			{	
				oSubSection = new SubSection;
				
				oSubSection.id = i.attributes()[1];
				oSubSection.title = i.attributes()[0];
				
				aSubArray.push(oSubSection);
			}
			
			return aSubArray; 
		}
		
		public static function parseFeed(dat:XML):Array
		{
			
			var aFeedArray:Array = new Array;
			var oFeedCategory:Category;
			var indexLenght:int = 0;
			
			for each(var i in dat.children())
			{
				var catID:String;
				
				if (i.localName() == "id")
				{
					catID = String(i).substring(21, 23);
					
					if (catID.substring(1) == '&')
					{
						catID = catID.substring(0,1);
					}
				}
				
				if (i.localName() == 'entry')
				{
					oFeedCategory = new Category;
					
					for each(var z in i.children())
					{
						if (z.localName() == "id")
						{
							oFeedCategory.id =  z;
							
							trace('id' + z);
						}
						
						if (z.localName() == "title")
						{
							oFeedCategory.title = z;
						}
						
						if (z.localName() == "summary")
						{
							oFeedCategory.summary = z;
						}
						
						if (z.localName() == "type")
						{
							oFeedCategory.type = z;
						}
						
						if (z.localName() == "updated")
						{
							oFeedCategory.updated = z;
						}
						
						if (z.localName() == "category")
						{
							trace('attributes: '  + z.attributes()[1]);
							
							trace(oFeedCategory.category);
						}
						
						if (z.localName() == "hits")
						{
							oFeedCategory.hits = z;
						}
						
						if (z.localName() == "link" && z.@rel == "alternate")
						{
							oFeedCategory.linkAlternate = z.@href;
						}
						
						if (z.localName() == "link" && z.@rel == "viewstats")
						{
							oFeedCategory.linkViewstats = z.@href;
						}
						
						if (z.localName() == "link" && z.@rel == "progress")
						{
							oFeedCategory.linkProgress = z.@href;
						}
						
						if (z.localName() == "link" && z.@rel == "related")
						{
							oFeedCategory.linkRelatedUrl = z.@href;
							oFeedCategory.linkRelatedTitle = z.@title;
						}
						
						if (z.localName() == "group")
						{
							oFeedCategory.group = parseMediaGroup(z);
						}
					}
					
					aFeedArray.push(oFeedCategory);		
				}
			}
			
			return aFeedArray;
		}
		
		public static function parseMediaGroup(obj:XML):Array
		{
			var aMediaGroupArray:Array = new Array();
			var oMediaGroup:MediaGroup
			
			oMediaGroup = new MediaGroup;
			
			for each(var i in obj.children())
			{
				if (i.localName() == "content")
				{
					oMediaGroup.mediaContentVidUrl = i.@url;
					oMediaGroup.mediaContentVidDuration = i.@duration;
					oMediaGroup.mediaContentVidBitrate = i.@bitrate;
					oMediaGroup.mediaContentVidType = i.@type;
				}
				
				if (i.localName() =="splash" &&  i.@width == "768")
				{
					oMediaGroup.mediaSplashBig = i.@url;
				}
				
				if (i.localName() =="splash" && i.@width == "256")
				{
					oMediaGroup.mediaSplashMedium = i.@url;
				}
				
				if (i.localName() =="splash" && i.@width == "140")
				{
					oMediaGroup.mediaSplashSmall = i.@url;
				}
				
				if (i.localName() =="splash" && i.@width == "72")
				{
					oMediaGroup.mediaSplashSmallest = i.@url;
					
				}
				
				if (i.localName() =="thumbnail" && i.@width == "768")
				{
					oMediaGroup.mediaSplashBigThumb = i.@url;
					
				}
				
				if (i.localName() =="thumbnail" && i.@width == "260")
				{
					oMediaGroup.mediaSplashMediumThumb = i.@url;
				}
				
				if (i.localName() =="thumbnail" && i.@width == "140")
				{
					oMediaGroup.mediaSplashSmallThumb = i.@url;
				}
				
				if (i.localName() == "thumbnail" && i.@width == "72")
				{
					oMediaGroup.mediaSplashSmallestThumb = i.@url;
				}
			}
			
			aMediaGroupArray.push(oMediaGroup);
			
			return aMediaGroupArray;
		}
		
		public static function parseData(obj:Array):Array
		{
			var index:int = 0;
			var objArray:Array = new Array();
			
			for each(var oCat:Category in obj)
			{
				for each(var oMedia:MediaGroup in oCat.group)
				{
					objArray[index] = [oCat,oMedia];
				}
				
				index++;
			}
			
			return objArray; 
		}
	}
}